﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.*;
	import com.wandian.data.GameData;
	import com.wandian.model.*;
	import com.wandian.model.vo.*;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZLlistScrollPanel;
	import com.wandian.util.CLZTiming;
	import com.wandian.util.ContextMenuPlus;
	import com.wandian.view.components.ui.RoomPlayerInfoMc;
	import com.wandian.view.components.ui.ScrollBarTxt;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.ui.ContextMenu;
	import flash.ui.Keyboard;

	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class SystemChatMediators extends Mediator
	{
		private var _inRoomData:InRoomPlayerDataProxy;
		private var _fastPanel:Sprite;
		private var _playerPanel:Sprite;
		private var _global:GameData;
		private var _scrollSystem:ScrollBarTxt;
		private var _scrollChat:ScrollBarTxt;
		private var _isSendChat:Boolean=true;
		private var contextMenu:ContextMenu;
		public static const NAME:String="SystemChatMediators";

		public function SystemChatMediators(param1:Object=null)
		{
			this._global=GameData.getInstance();
			super(NAME, param1);
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			return;
		}

		override public function onRegister():void
		{
			var _loc_1:*=GameData.getMovieClip("ScrollBarTxt");
			this.systemMsgMc.addChild(_loc_1);
			_loc_1.x=this.systemMsgMc.systemMsg_txt.x + this.systemMsgMc.systemMsg_txt.width;
			_loc_1.y=this.systemMsgMc.systemMsg_txt.y;
			this._scrollSystem=new ScrollBarTxt(this.systemMsgMc.systemMsg_txt, _loc_1);
			var _loc_2:*=GameData.getMovieClip("ScrollBarTxt");
			this.systemMsgMc.addChild(_loc_2);
			_loc_2.x=this.systemMsgMc.chatMsg_txt.x + this.systemMsgMc.chatMsg_txt.width;
			_loc_2.y=this.systemMsgMc.chatMsg_txt.y;
			this._scrollChat=new ScrollBarTxt(this.systemMsgMc.chatMsg_txt, _loc_2);
			this.systemMsgMc.send_btn.addEventListener(MouseEvent.CLICK, this.onSendChatHandler);
			Texas.I._hotkey.registerGlobalKey(Keyboard.ENTER, this.onSendChatHandler);
			Texas.I._mcGameView.stage.focus=this.systemMsgMc.input_txt;
			this.systemMsgMc.fast_btn.addEventListener(MouseEvent.CLICK, this.onFastChatHandler);
			this.systemMsgMc.chatMsg_txt.addEventListener(TextEvent.LINK, this.linkTxtHandler);
			CLZButton.setButton(this.systemMsgMc.roomPlayer_btn, "", {tip: GameData.tip["ROOM_PLAYER_LIST"], time: 10, pointXY: {disX: 0, disY: -30}});
			this.systemMsgMc.roomPlayer_btn.addEventListener(MouseEvent.CLICK, this.onRoomPlayerHandler);
			this.initContextMenu();
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.CHAT_SYSTEM_TIP, CLZNoticeName.CHAT_RES, CLZNoticeName.GET_REWARD_CORNUCOPIA, CLZNoticeName.ON_STAGE, CLZNoticeName.ONLIN_FRIEND, CLZNoticeName.OFF_FRIEND, CLZNoticeName.OUT_GAME_ROOM_RSP];
		}

		override public function handleNotification(notification:INotification):void
		{
			var data:Object=null;
			var roomName:String=null;
			var cardType:String=null;
			var RewardCornucopiaMsg:String=null;
			var _loc_6:Object=null;
			var friendVo:FriendVo=null;
			var accountProxy:AccountProxy=null;
			data=notification.getBody();
			switch (notification.getName())
			{
				case CLZNoticeName.CHAT_SYSTEM_TIP:
				{
					this.showSystem(data as String);
					break;
				}
				case CLZNoticeName.CHAT_RES:
				{
					switch (data.type)
					{
						case 1:
						{
							if (data.userid == 0)
							{
								this.showMsg("<font color=\'#ffff00\'>" + GameData.tip["SYSTEM_NAME"] + "：" + data.msg + "</font>");
							}
							else
							{
								accountProxy=facade.retrieveProxy(AccountProxy.NAME) as AccountProxy;
								accountProxy.subProp(1, 1);
								this.showMsg("<font color=\'#ffff00\'>" + GameData.tip["HORN_NAME_TIP"] + " " + data.name + "：" + data.msg + "</font>");
							}
							break;
						}
						case 2:
						{
							this.showMsg("<font color=\'#ffff00\'>" + GameData.tip["SYSTEM_NAME"] + "：" + data.msg + "</font>");
							break;
						}
						case 3:
						{
							this.showMsg("<font color=\'#ffff00\'>" + data.msg + "</font>");
							break;
						}
						default:
						{
							if (data.userid == 0)
							{
								this.showMsg(data.msg);
							}
							else
							{
								this.showMsg("<font color=\'#FFFFCC\'>" + data.name + "</font>" + "：" + data.msg);
							}
							break;
						}
							this.sendNotification(CLZNoticeName.CHAT_SYSTEM_TIP, data.msg);
					}
					break;
				}
				case CLZNoticeName.GET_REWARD_CORNUCOPIA:
				{
					roomName=GameData.getRoomInfoById(GameData.ALL, data.roomId).name;
					cardType=GameData.tip["cardType"].split("|")[data.cardType];
					if (this._global.userInfo.userVo.userid == data.userid)
					{
						this._global.userInfo.money=data.money + this._global.userInfo.userVo.money;
						RewardCornucopiaMsg=GameData.replaceStrValue(GameData.tip["GET_REWARD_CORNUCOPIA_1"], "ROOM_NAME", roomName);
						RewardCornucopiaMsg=GameData.replaceStrValue(RewardCornucopiaMsg, "CARD_TYPE", cardType);
						RewardCornucopiaMsg=GameData.replaceStrValue(RewardCornucopiaMsg, "GOLD", data.money);
						this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: RewardCornucopiaMsg});
					}
					else
					{
						RewardCornucopiaMsg=GameData.replaceStrValue(GameData.tip["GET_REWARD_CORNUCOPIA_2"], "ROOM_NAME", roomName);
						RewardCornucopiaMsg=GameData.replaceStrValue(RewardCornucopiaMsg, "PLAYER_NAME", data.name);
						RewardCornucopiaMsg=GameData.replaceStrValue(RewardCornucopiaMsg, "CARD_TYPE", cardType);
						RewardCornucopiaMsg=GameData.replaceStrValue(RewardCornucopiaMsg, "GOLD", data.money);
					}
					this.sendNotification(CLZNoticeName.CHAT_RES, {type: 2, msg: RewardCornucopiaMsg});
					break;
				}
				case CLZNoticeName.ON_STAGE:
				{
					trace(CLZNoticeName.ON_STAGE);
					if (!GameData.checkStageHitTest(this._fastPanel, data.point, this.systemMsgMc.fast_btn))
					{
						this.removePanel(this._fastPanel);
						this._fastPanel=null;
					}
					if (!GameData.checkStageHitTest(this._playerPanel, data.point, this.systemMsgMc.roomPlayer_btn))
					{
						this.removePanel(this._playerPanel);
						this._playerPanel=null;
					}
					break;
				}
				case CLZNoticeName.OUT_GAME_ROOM_RSP: //聊天框移除
				{
					if (this._global.userInfo.userVo.userid == data.userid)
					{
						this.removePanel(this._playerPanel);
						this._playerPanel=null;
						this.removePanel(this._fastPanel);
						this._fastPanel=null;
					}
					break;
				}
				case CLZNoticeName.ONLIN_FRIEND:
				{
					friendVo=data as FriendVo;
					this.sendNotification(CLZNoticeName.CHAT_RES, {type: 3, msg: GameData.replaceStrValue(GameData.tip["YOU_FRIEND_ONLINE"], "NAME", friendVo.name)});
					break;
				}
				case CLZNoticeName.OFF_FRIEND:
				{
					friendVo=data as FriendVo;
					this.sendNotification(CLZNoticeName.CHAT_RES, {type: 3, msg: GameData.replaceStrValue(GameData.tip["YOU_FRIEND_OFFLINE"], "NAME", friendVo.name)});
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function onSendChatHandler(event:MouseEvent=null):void
		{
			var sendMsg:String=this.systemMsgMc.input_txt.text;
			this.systemMsgMc.input_txt.text="";
//			sendMsg=sendMsg.replace(new RegExp(" ", "gi"), "");
			if (sendMsg.replace(new RegExp(" ", "gi"), "") == "")
			{
				return;
			}
			sendMsg=sendMsg.replace(new RegExp("  ", "gi"), "");
			if (this._isSendChat)
			{
				this._isSendChat=false;
				this.sendNotification(CLZNoticeName.CHAT, {msg: sendMsg, type: 0});
				CLZTiming.add(0.5, this.okSendChat);
			}
			else
			{
				this.sendNotification(CLZNoticeName.CHAT_RES, {msg: "<font color=\'#666666\'>" + GameData.tip["WAIT_CHAT_TIP"] + "<font color=\'#cccccc\'>", type: 0, mid: 0});
			}
			return;
		}

		private function okSendChat():void
		{
			this._isSendChat=true;
			CLZTiming.remove(this.okSendChat);
			return;
		}

		private function showMsg(param1:String):void
		{
			param1=param1.replace(new RegExp("\n", "gi"), "");
			if (this.systemMsgMc.chatMsg_txt.numLines > 50)
			{
				this.systemMsgMc.chatMsg_txt.replaceText(0, this.systemMsgMc.chatMsg_txt.getLineOffset(30), "");
			}
			this.systemMsgMc.chatMsg_txt.htmlText=this.systemMsgMc.chatMsg_txt.htmlText + (param1 + "\n");
			this.systemMsgMc.chatMsg_txt.scrollV=this.systemMsgMc.chatMsg_txt.maxScrollV;
			return;
		}

		private function init(event:Event):void
		{
			var evt:*=event;
			try
			{
			}
			catch (e:Error)
			{
			}
			return;
		}

		private function showSystem(param1:String):void
		{
			this.systemMsgMc.systemMsg_txt.htmlText=this.systemMsgMc.systemMsg_txt.htmlText + (GameData.tip["SYSTEM_NAME"] + "：" + param1 + "\n");
			if (this.systemMsgMc.systemMsg_txt.numLines > 50)
			{
				this.systemMsgMc.systemMsg_txt.replaceText(0, this.systemMsgMc.systemMsg_txt.getLineOffset(30), "");
			}
			this.systemMsgMc.systemMsg_txt.scrollV=this.systemMsgMc.systemMsg_txt.maxScrollV;
			return;
		}

		private function onFastChatHandler(event:MouseEvent):void
		{
			var _loc_2:CLZLlistScrollPanel=null;
			var _loc_3:Array=null;
			var _loc_4:int=0;
			var _loc_5:int=0;
			var _loc_6:MovieClip=null;
			if (this._fastPanel == null)
			{
				this._fastPanel=GameData.getSprite("FastPanel");
				Texas.I._mcWindosView.addChild(this._fastPanel);
				this._fastPanel.x=719;
				this._fastPanel.y=265;
				_loc_2=new CLZLlistScrollPanel(1, 183, 24, 0, 0, 243, 188);
				this._fastPanel.addChild(_loc_2);
				_loc_3=GameData.getFastMsgXML();
				_loc_4=_loc_3.length;
				_loc_5=0;
				while (_loc_5 < _loc_4)
				{

					_loc_6=GameData.getMovieClip("FastInfoMc");
					_loc_2.setMc(_loc_6, _loc_5, _loc_4);
					CLZButton.setButton(_loc_6, _loc_3[_loc_5]);
					_loc_6.addEventListener(MouseEvent.CLICK, this.changeMcInfoHandler);
					_loc_5=_loc_5 + 1;
				}
			}
			else
			{
				this.removePanel(this._fastPanel);
				this._fastPanel=null;
			}
			return;
		}

		private function changeMcInfoHandler(event:MouseEvent):void
		{
			var _loc_2:*=event.target as MovieClip;
			this.sendNotification(CLZNoticeName.CHAT, {msg: _loc_2.txt.text, type: 0});
			this.removePanel(this._fastPanel);
			this._fastPanel=null;
			Texas.I._mcGameView.stage.focus=this.systemMsgMc.input_txt;
			return;
		}

		private function onRoomPlayerHandler(event:MouseEvent):void
		{
			var _loc_2:CLZLlistScrollPanel=null;
			var _loc_3:Array=null;
			var _loc_4:int=0;
			var _loc_5:int=0;
			var _loc_6:MovieClip=null;
			var _loc_7:RoomPlayerInfoMc=null;
			trace("onRoomPlayerHandler");
			if (this._playerPanel == null)
			{
				trace("_playerPanel null");
				this._playerPanel=GameData.getSprite("RoomPlayer");
				Texas.I._mcWindosView.addChild(this._playerPanel);
				this._playerPanel["title_txt"].text=GameData.tip["THIS_ROOM_ALL_PLAYER"];
				this._playerPanel.x=677;
				this._playerPanel.y=146;
				_loc_2=new CLZLlistScrollPanel(1, 220, 24, 0, 0, 313, 220);
				this._playerPanel.addChild(_loc_2);
				_loc_2.x=5;
				_loc_2.y=40;
				_loc_3=this._inRoomData.roomPlayer;
				_loc_4=_loc_3.length;
				_loc_5=0;
				while (_loc_5 < _loc_4)
				{

					_loc_6=GameData.getMovieClip("PlayerBtn");
					_loc_2.setMc(_loc_6, _loc_5, _loc_4);
					_loc_7=new RoomPlayerInfoMc(_loc_6, _loc_3[_loc_5]);
					_loc_7.addEventListener(Event.CHANGE, this.changeRoomPlayerPanelHandler);
					_loc_5=_loc_5 + 1;
				}
			}
			else
			{
				trace("_playerPanel removePanel");
				this.removePanel(this._playerPanel);
				this._playerPanel=null;
			}
			return;
		}

		private function changeRoomPlayerPanelHandler(event:Event):void
		{
			this.removePanel(this._playerPanel);
			this._playerPanel=null;
			return;
		}

		private function linkTxtHandler(event:TextEvent):void
		{
			var _loc_2:*=event.text.split("-");
			var _loc_3:*=new Object();
			_loc_3.playerVo=this._inRoomData.getInRoomPlayer(this._global.userInfo.userVo.userid);
			_loc_3.pcate=_loc_2[0];
			_loc_3.pframe=_loc_2[1];
			this.sendNotification(CLZNoticeName.SHOW_USE_SELECTED_TIP, _loc_3);
			return;
		}

		private function removePanel(panel:Sprite):void
		{
			if (panel != null)
			{
				if (Texas.I._mcWindosView.contains(panel))
				{
					Texas.I._mcWindosView.removeChild(panel);
					panel=null;
				}
			}
			return;
		}

		public function initContextMenu():void
		{
			this.contextMenu=new ContextMenuPlus(true, this.removeMsg, GameData.tip["REMOVE_CHAT"]).contextMenu;
			this.systemMsgMc.parent.parent.parent.contextMenu=this.contextMenu;
			return;
		}

		private function removeMsg(event:ContextMenuEvent):void
		{
			this.systemMsgMc.chatMsg_txt.text="";
			return;
		}

		private function get systemMsgMc():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
